package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.common.support.CustomLoadDialog;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.dal.IUserDataDAL;
import com.qire.manhua.model.viewModel.impl.AccountsAssociatedAddViewModel;
import com.qire.manhua.model.vo.AccountAssociatedVerifyVO;
import com.qire.manhua.model.vo.OkResponseVO;
import com.qire.other.thirdPlatform.QqPlatform;
import com.qire.other.thirdPlatform.WeChatPlatform;

public class AccountsAssociatedAddService extends DataService {

    private final IUserDataDAL userDataDAL = WarehouseFactory.create(this, IUserDataDAL.class);

    private final AccountsAssociatedAddViewModel viewModel;

    private String accountType;
    private String accountValue;

    public AccountsAssociatedAddService(AccountsAssociatedAddViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**
     * 获取验证码
     * @param phoneNumber 接收的手机号码
     */
    public void fetchVerifyCode(String phoneNumber) {
        userDataDAL.fetchVerificationCode(phoneNumber);
    }
    @StrategyAnnotation(event = Url.FetchVerificationCode)
    public void fetchVerificationCodeCallback(OkResponseVO okVO, boolean isCache) {
        ToastHelper.showSnackBar("验证码已发送请注意查收！", Prompt.SUCCESS);
    }

    /**
     * 校验验证码
     * @param phoneNumber 接收的手机号码
     * @param verifyCode 接收的验证码
     */
    public void checkVerificationCode(String phoneNumber, String verifyCode) {
        userDataDAL.checkVerificationCode(phoneNumber, verifyCode);
    }
    @StrategyAnnotation(event = Url.CheckVerificationCode)
    public void checkVerificationCodeCallBack(OkResponseVO responseOkVO, boolean isCache) {
        ToastHelper.showSnackBar("验证成功！", Prompt.SUCCESS);
        viewModel.setAddUsedValidateCode(responseOkVO.validate);
        viewModel.gotoPanel(3);
    }

    /**
     * 验证关联账户通过手机号码
     * @param phoneNumber 验证关联的手机号码
     */
    public void accountVerifyByPhone(String phoneNumber) {
        accountType = "1";
        accountValue = phoneNumber;
        userDataDAL.accountAssociatedVerify(accountType, accountValue);
    }
    /**
     * 验证关联账户通过微信
     */
    public void accountVerifyByWeChat() {
        WeChatPlatform.WeChat.authorize((msg, info) -> {
            if(ObjectUtil.nonNull(info)) {
                accountType = "2";
                accountValue = info.openid;
                userDataDAL.accountAssociatedVerify(accountType, accountValue);
            } else {
                ToastHelper.showToast(msg);
            }
            CustomLoadDialog.dismissDialog();
        });

    }
    /**
     * 验证关联账户通过QQ
     */
    public void accountVerifyByQQ() {
        QqPlatform.build().authorize((msg, info) -> {
            if(ObjectUtil.nonNull(info)) {
                accountType = "3";
                accountValue = info.openid;
                userDataDAL.accountAssociatedVerify(accountType, accountValue);
            } else {
                ToastHelper.showToast(msg);
            }
            CustomLoadDialog.dismissDialog();
        });
    }
    @StrategyAnnotation(event = {Url.AccountAssociatedVerify})
    public void accountAssociatedVerifyCallback(AccountAssociatedVerifyVO dataVO, boolean isCache) {
        if(dataVO.asso_verify != 1) {
            viewModel.showResultPanel(false, dataVO.tips);
            return;
        }
        viewModel.setAssociatedUserInfo(dataVO.user.user_id, dataVO.user.user_img, dataVO.user.user_name, dataVO.user.user_hash);

        if("1".equals(accountType)) {
            viewModel.gotoPanel(2);
            viewModel.fetchVerifyCode();
        } else {
            viewModel.gotoPanel(3);
        }
    }

    /**
     * 添加关联账户
     */
    public void accountAssociatedAdd(int userId, String userHash, String validateCode) {
        userDataDAL.accountAssociatedAdd(accountType, accountValue, userId, userHash, validateCode);
    }
    @StrategyAnnotation(event = {Url.AccountAssociatedAdd})
    public void accountAssociatedAddCallback(OkResponseVO dataVO, boolean isCache) {
        ToastHelper.showToast(dataVO.ok);
        viewModel.showResultPanel(true, dataVO.ok);
    }

}
